"use client"

import { MoreHorizontal, Share, Plus, Trash, Download, Flag } from "lucide-react"
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { useRouter } from "next/navigation"
import { toast } from "@/components/ui/use-toast"

export interface ShareableResource {
    id: number | string
    type: "song" | "album" | "playlist" | "mv" | "artist"
    title: string
    cover?: string
    artist?: string
}

interface MoreOptionsMenuProps {
    resource: ShareableResource
    onAddToPlaylist?: () => void
    onDownload?: () => void
    onDelete?: () => void
    onReport?: () => void
    className?: string
    align?: "start" | "center" | "end"
    side?: "top" | "right" | "bottom" | "left"
}

export function MoreOptionsMenu({
                                    resource,
                                    onAddToPlaylist,
                                    onDownload,
                                    onDelete,
                                    onReport,
                                    className,
                                    align = "end",
                                    side = "bottom",
                                }: MoreOptionsMenuProps) {
    const router = useRouter()

    const handleShare = () => {
        try {
            // 将资源信息存储在 sessionStorage 中
            sessionStorage.setItem("shareResource", JSON.stringify(resource))

            // 跳转到分享页面
            router.push("/community/share")
        } catch (error) {
            console.error("Failed to prepare share:", error)
            toast({
                title: "准备分享失败",
                description: "请稍后重试"
            })
        }
    }

    const handleCopyLink = () => {
        try {
            // 根据资源类型构建URL
            let url = window.location.origin

            switch (resource.type) {
                case "song":
                    url += `/song/${resource.id}`
                    break
                case "album":
                    url += `/album/${resource.id}`
                    break
                case "playlist":
                    url += `/playlist/${resource.id}`
                    break
                case "mv":
                    url += `/mv/${resource.id}`
                    break
                case "artist":
                    url += `/artist/${resource.id}`
                    break
                default:
                    url += `/${resource.type}/${resource.id}`
            }

            navigator.clipboard.writeText(url)
            toast({
                title: "链接已复制",
                description: "链接已复制到剪贴板",
            })
        } catch (error) {
            toast({
                title: "复制失败",
                description: "请稍后重试"
            })
        }
    }

    return (
        <DropdownMenu>
            <DropdownMenuTrigger asChild>
                <Button variant="ghost" size="icon" className={cn("h-8 w-8", className)}>
                    <MoreHorizontal className="h-4 w-4" />
                    <span className="sr-only">更多选项</span>
                </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align={align} side={side} className="w-48">
                <DropdownMenuItem onClick={handleShare} className="cursor-pointer">
                    <Share className="mr-2 h-4 w-4" />
                    <span>分享到社区</span>
                </DropdownMenuItem>

                <DropdownMenuItem onClick={handleCopyLink} className="cursor-pointer">
                    <Share className="mr-2 h-4 w-4" />
                    <span>复制链接</span>
                </DropdownMenuItem>

                {onAddToPlaylist && (
                    <DropdownMenuItem onClick={onAddToPlaylist} className="cursor-pointer">
                        <Plus className="mr-2 h-4 w-4" />
                        <span>添加到歌单</span>
                    </DropdownMenuItem>
                )}

                {onDownload && (
                    <DropdownMenuItem onClick={onDownload} className="cursor-pointer">
                        <Download className="mr-2 h-4 w-4" />
                        <span>下载</span>
                    </DropdownMenuItem>
                )}

                {onDelete && (
                    <>
                        <DropdownMenuSeparator />
                        <DropdownMenuItem onClick={onDelete} className="cursor-pointer text-destructive">
                            <Trash className="mr-2 h-4 w-4" />
                            <span>删除</span>
                        </DropdownMenuItem>
                    </>
                )}

                {onReport && (
                    <DropdownMenuItem onClick={onReport} className="cursor-pointer">
                        <Flag className="mr-2 h-4 w-4" />
                        <span>举报</span>
                    </DropdownMenuItem>
                )}
            </DropdownMenuContent>
        </DropdownMenu>
    )
}

